package com.shelpe.services.lexicon.dto;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.lexicon.constants.Device;
import com.shelpe.services.lexicon.entity.KeywordRecommend;
import com.shelpe.services.lexicon.entity.KeywordTag;
import com.shelpe.services.lexicon.entity.metrics.InsightMetrics;

import lombok.Data;

@Data
@JsonInclude(Include.NON_NULL)
public class RecommendWordDto {
	private final static String[] TEMPLATE = new String[0];
	
	private String word;
	
	private int pertinence;//相关性
	private int competition;//竞争度
	private int impression;//展现量
	
	@JsonProperty("pv")
	private int pageVolume;
	@JsonProperty("avg_price")
	private float averagePrice;//平均价格
	@JsonProperty("recommend_price")
	private float recommendPrice;//平均价格
	private float price;
	
	private int click;//点击量
	@JsonProperty("ctr")
	private float clickRatio;//点击率
	@JsonProperty("coverage")
	private float clickCoverage;//点击转化率
	
	@JsonIgnore
	private String recommendID;
	
	@JsonProperty(value = "tag_name")
	private String[] tagNames;
	
	@JsonProperty(value = "tag_display")
	private String[] tagDisplayNames;
	
	private String origin;
	private boolean centeral;
	
	public void from(KeywordRecommend recommend, Device device){
		if(recommend != null){
			BeanUtils.copyProperties(recommend, this);
			this.price = ((float) recommend.getPrice()) / 100;
			this.origin = recommend.getOrigin().name();
			if(recommend.getMetric() != null){
				BeanUtils.copyProperties(recommend.getMetric(), this);
				if(recommend.getMetric().getAveragePrice() != null){
					this.recommendPrice = recommend.getMetric().getAveragePrice().floatValue();
				}
			}
			InsightMetrics insight = ((device == Device.mobile)?recommend.getMobileInsight():recommend.getInsight());
			
			
			if(insight != null){
				BeanUtils.copyProperties(insight, this);
				if(insight.getCtr() != null){
					this.clickCoverage = insight.getCtr().floatValue();
					this.clickRatio =  insight.getCtr().floatValue();
				}
				this.pageVolume = insight.getImpression();
				if(insight.getCpc() != null){
					this.averagePrice = insight.getCpc().floatValue() / 100;
				}
			}
			
		}
	}
	
	public void from(List<KeywordTag> tags){
		if(tags != null){
			List<String> names = new ArrayList<String>();
			List<String> displays = new ArrayList<String>();
			for(KeywordTag tag:tags){
				names.add(tag.getTagName());
				if(tag.getDisplayName() != null){
					displays.add(tag.getDisplayName());
				}
			}
			this.tagNames = names.toArray(TEMPLATE);
			this.tagDisplayNames = displays.toArray(TEMPLATE);
		}
	}
}
